const StateManager = require("../template")

function testStateManagerPerformance() {
  console.log(
    "1. 大量状态设置性能测试:",
    (() => {
      const stateManager = new StateManager()
      const stateCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < stateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      const end = performance.now()
      const setTime = end - start
      
      return setTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "2. 大量状态获取性能测试:",
    (() => {
      const stateManager = new StateManager()
      const stateCount = 1000
      
      // 设置状态
      for (let i = 0; i < stateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      const start = performance.now()
      
      // 获取状态
      for (let i = 0; i < stateCount; i++) {
        stateManager.getState(`key${i}`)
      }
      
      const end = performance.now()
      const getTime = end - start
      
      return getTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "3. 大量订阅性能测试:",
    (() => {
      const stateManager = new StateManager()
      const subscribeCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < subscribeCount; i++) {
        stateManager.subscribe(`key${i}`, () => {})
      }
      
      const end = performance.now()
      const subscribeTime = end - start
      
      return subscribeTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "4. 大量状态更新性能测试:",
    (() => {
      const stateManager = new StateManager()
      const updateCount = 1000
      
      // 设置初始状态
      for (let i = 0; i < updateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      const start = performance.now()
      
      // 更新状态
      for (let i = 0; i < updateCount; i++) {
        stateManager.updateState(`key${i}`, (prev) => prev + 1)
      }
      
      const end = performance.now()
      const updateTime = end - start
      
      return updateTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "5. 混合操作性能测试:",
    (() => {
      const stateManager = new StateManager()
      const operationCount = 1000
      
      const start = performance.now()
      
      for (let i = 0; i < operationCount; i++) {
        const operation = Math.floor(Math.random() * 4)
        
        if (operation === 0) {
          stateManager.setState(`key${i}`, i)
        } else if (operation === 1) {
          stateManager.getState(`key${i}`)
        } else if (operation === 2) {
          stateManager.subscribe(`key${i}`, () => {})
        } else {
          stateManager.updateState(`key${i}`, (prev) => prev + 1)
        }
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000 // 1秒内完成
    })()
  )

  console.log(
    "6. 状态管理器内存效率测试:",
    (() => {
      const stateManager = new StateManager()
      const stateCount = 1000
      
      // 添加大量状态
      for (let i = 0; i < stateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      // 验证内存使用合理
      const statePaths = stateManager.getStatePaths()
      
      return statePaths.length === stateCount
    })()
  )

  console.log(
    "7. 状态管理器并发操作性能测试:",
    (() => {
      const stateManager = new StateManager()
      const operationCount = 1000
      
      const start = performance.now()
      
      // 并发操作
      for (let i = 0; i < operationCount; i++) {
        stateManager.setState(`key${i}`, i)
        stateManager.getState(`key${i}`)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "8. 状态管理器批量操作性能测试:",
    (() => {
      const stateManager = new StateManager()
      const batchSize = 100
      const batches = 10
      
      const start = performance.now()
      
      // 批量操作
      for (let batch = 0; batch < batches; batch++) {
        const updates = {}
        for (let i = 0; i < batchSize; i++) {
          updates[`batch${batch}_key${i}`] = batch * batchSize + i
        }
        stateManager.batchUpdate(updates)
      }
      
      const end = performance.now()
      const totalTime = end - start
      
      return totalTime < 1000
    })()
  )

  console.log(
    "9. 状态管理器快照性能测试:",
    (() => {
      const stateManager = new StateManager()
      const stateCount = 1000
      
      // 添加状态
      for (let i = 0; i < stateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      const start = performance.now()
      
      // 快照操作
      const snapshot = stateManager.getSnapshot()
      stateManager.restoreSnapshot(snapshot)
      
      const end = performance.now()
      const snapshotTime = end - start
      
      return snapshotTime < 1000
    })()
  )

  console.log(
    "10. 状态管理器路径查询性能测试:",
    (() => {
      const stateManager = new StateManager()
      const stateCount = 1000
      
      // 添加状态
      for (let i = 0; i < stateCount; i++) {
        stateManager.setState(`key${i}`, i)
      }
      
      const start = performance.now()
      
      // 路径查询
      const paths = stateManager.getStatePaths()
      
      const end = performance.now()
      const queryTime = end - start
      
      return paths.length === stateCount &&
             queryTime < 1000
    })()
  )

  console.log(
    "11. this上下文性能测试:",
    (() => {
      const perfObj = {
        multiplier: 2,
        processPerformance: function(count) {
          const stateManager = new StateManager()
          const start = performance.now()
          
          for (let i = 0; i < count; i++) {
            stateManager.setState(`key${i}`, i)
          }
          
          const end = performance.now()
          const time = end - start
          
          return {
            time: time,
            result: count * this.multiplier
          }
        }
      }
      
      const result = perfObj.processPerformance(100)
      
      return result.result === 200 &&
             result.time < 100
    })()
  )

  console.log(
    "12. 状态管理器性能一致性测试:",
    (() => {
      const stateManager1 = new StateManager()
      const stateManager2 = new StateManager()
      const stateCount = 100
      
      // 相同操作测试一致性
      const times1 = []
      const times2 = []
      
      for (let i = 0; i < 10; i++) {
        const start1 = performance.now()
        for (let j = 0; j < stateCount; j++) {
          stateManager1.setState(`key${j}`, j)
        }
        const end1 = performance.now()
        times1.push(end1 - start1)
        
        const start2 = performance.now()
        for (let j = 0; j < stateCount; j++) {
          stateManager2.setState(`key${j}`, j)
        }
        const end2 = performance.now()
        times2.push(end2 - start2)
      }
      
      // 验证性能一致性
      const avgTime1 = times1.reduce((a, b) => a + b, 0) / times1.length
      const avgTime2 = times2.reduce((a, b) => a + b, 0) / times2.length
      
      return avgTime1 < 100 &&
             avgTime2 < 100 &&
             Math.abs(avgTime1 - avgTime2) < 50
    })()
  )
}

testStateManagerPerformance()
